<!-- src/views/OrderView.vue -->
<template>
  <div class="order-page">
    <!-- 顶部导航栏 -->
    <TopNav />

    <div class="order-container" v-if="displayOrder">
      <h1>订单详情</h1>

      <div class="order-info">
        <div class="info-item">
          <span class="label">订单编号：</span>
          <span>{{ displayOrder.id }}</span>
        </div>
        <div class="info-item">
          <span class="label">下单时间：</span>
          <span>{{ displayOrder.date }}</span>
        </div>
        <div class="info-item">
          <span class="label">下单用户：</span>
          <span>{{ displayOrder.username }}</span>
        </div>
        <div class="info-item">
          <span class="label">收件人：</span>
          <span>{{ displayOrder.recipient }}</span>
        </div>
        <div class="info-item">
          <span class="label">联系电话：</span>
          <span>{{ displayOrder.phone }}</span>
        </div>
        <div class="info-item">
          <span class="label">配送地址：</span>
          <span>{{ displayOrder.address }}</span>
        </div>
      </div>

      <div class="order-items">
        <el-table :data="displayOrder.items" style="width: 100%">
          <el-table-column label="商品图片" width="150">
            <template #default="scope">
              <img :src="scope.row.gImage" alt="商品图片" class="item-image" />
            </template>
          </el-table-column>
          <el-table-column label="商品名称" width="400" prop="gName" />
          <el-table-column label="单价" width="150">
            <template #default="scope">
              ￥{{ scope.row.gPrice }}
            </template>
          </el-table-column>
          <el-table-column label="数量" width="150" prop="quantity" />
          <el-table-column label="小计" width="200">
            <template #default="scope">
              ￥{{ (scope.row.gPrice * scope.row.quantity).toFixed(2) }}
            </template>
          </el-table-column>
        </el-table>
      </div>

      <div class="order-total">
        <span>订单总价：￥{{ displayOrder.total.toFixed(2) }}</span>
      </div>

      <div class="actions">
        <el-button @click="goToOrderList">查看所有订单</el-button>
        <el-button type="primary" @click="continueShopping">继续选购</el-button>
      </div>
    </div>

    <div class="empty-order" v-else>
      <p>暂无订单信息</p>
      <router-link to="/orders" class="go-to-orders">查看所有订单</router-link>
    </div>
  </div>
</template>

<script setup>
import { computed } from 'vue'
import { useRouter } from 'vue-router'
import { useStore } from 'vuex'
import TopNav from '@/components/TopNav.vue'

const store = useStore()
const router = useRouter()

// 获取要显示的订单信息（优先显示当前订单，否则显示最新订单）
const displayOrder = computed(() => {
  // 优先获取通过路由传递的订单，否则获取最新订单
  return store.getters.getCurrentOrder || store.getters.getOrder
})

// 继续购物
const continueShopping = () => {
  router.push('/home')
}

// 查看订单列表
const goToOrderList = () => {
  router.push('/orders')
}
</script>

<style scoped>
.order-page {
  max-width: 1200px;
  margin: 60px auto;
  padding: 20px;
  font-family: Arial, sans-serif;
  background-color: #f9f9f9;
}

.order-container {
  background-color: #fff;
  border-radius: 8px;
  box-shadow: 0 4px 8px rgba(0, 0, 0, 0.1);
  padding: 30px;
}

.order-container h1 {
  text-align: center;
  margin-bottom: 30px;
  color: #333;
  font-size: 24px;
}

.order-info {
  background-color: #e8f5e9;
  padding: 20px;
  border-radius: 8px;
  margin-bottom: 30px;
  display: grid;
  grid-template-columns: repeat(2, 1fr);
  gap: 20px;
}

.info-item {
  display: flex;
  align-items: center;
  font-size: 16px;
}

.label {
  font-weight: bold;
  margin-right: 10px;
  width: 100px;
  display: inline-block;
  color: #333;
}

.item-image {
  width: 120px;
  height: auto;
  border-radius: 4px;
}

.order-items {
  margin-bottom: 30px;
}

.order-total {
  text-align: right;
  font-size: 18px;
  font-weight: bold;
  margin: 20px 0;
  border-top: 1px solid #eee;
  padding-top: 20px;
}

.actions {
  display: flex;
  justify-content: center;
  gap: 20px;
  margin-top: 30px;
}

.empty-order {
  text-align: center;
  margin-top: 100px;
}

.empty-order p {
  font-size: 18px;
  color: #666;
  margin-bottom: 20px;
}

.go-to-orders {
  display: inline-block;
  margin-top: 20px;
  padding: 10px 20px;
  background-color: #4caf50;
  color: white;
  border-radius: 4px;
  text-decoration: none;
  font-size: 16px;
  transition: background-color 0.3s;
}

.go-to-orders:hover {
  background-color: #3e8e41;
}

.el-button {
  font-size: 16px;
  padding: 12px 24px;
  border-radius: 4px;
  transition: all 0.3s;
}

.el-button:hover {
  opacity: 0.9;
}

.el-button--primary {
  background-color: #4caf50;
  border-color: #4caf50;
}

.el-button--primary:hover {
  background-color: #3e8e41;
  border-color: #3e8e41;
}
</style>